package com.nondo.dean.platform.controller.api.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.platform.domain.Zone;
import com.nondo.dean.platform.model.ZoneBean;
import com.nondo.dean.platform.model.param.ZoneParam;
import com.nondo.dean.platform.model.result.ZoneResult;
import com.nondo.dean.platform.service.ZoneService;

/**
 * 区域管理后台接口
 * @author zlj
 * @date 2018年4月3日
 */
@RestController("apiAdminZone")
@RequestMapping("/{v}/admin/zone")
public class ZoneController {
    
    @Autowired
    ZoneService zoneService;
    
    /**
     * 查看区域详情
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<Zone> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<Zone> view = new View<Zone>(20000, "获取数据成功");
        
        Zone zone = null;
        if (id != null) {
            zone = zoneService.selectZone(id);
        } else {
            zone = new Zone();
        }

        view.setData(zone);
        return view;
    }
    
    /**
     * 添加区域
     * @param request
     * @param response
     * @param param 区域信息 {@link ZoneParam}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<ZoneParam> add(HttpServletRequest request, HttpServletResponse response,
            @Validated @RequestBody ZoneParam param, BindingResult result) {
        View<ZoneParam> view = new View<ZoneParam>(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Zone zoneDb = this.zoneService.selectZoneByName(param.getName());
            if (zoneDb != null) {
                return View.of500Error("区域名称已存在");
            }
            this.zoneService.addZone(param);
        }
        return view;
    }

    /**
     * 更新区域
     * @param request
     * @param response
     * @param zone 区域信息 {@link Zone}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<ZoneParam> update(HttpServletRequest request,
            HttpServletResponse response,
            @Validated @RequestBody ZoneParam param, BindingResult result) {
        View<ZoneParam> view = new View<ZoneParam>(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            Zone zoneDb = this.zoneService.selectZoneByName(param.getName());
            if (zoneDb.getName().equals(param.getName()) && zoneDb.getId() != param.getId()) {
                return View.of500Error("区域名称已存在");
            }
            this.zoneService.updateZone(param,zoneDb);
        }

        return view;
    }

    /**
     * 获取区域分页查询列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<Zone>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Zone> query) {
        View<Page<Zone>> view = new View<Page<Zone>>(20000, "获取列表成功");

        Zone zone = query.getData();

        Page<Zone> page = new Page<Zone>(query.getPage(), query.getLimit(), zoneService.count(zone));
        
        List<Zone> zoneList = zoneService.getZoneList(query.getOffset(), query.getLimit(), zone);
        
        page.setContent(zoneList);
        
        view.setData(page);
        
        return view;
    }

    /**
     * 删除区域
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View view = new View(20000, "删除成功");

        Zone zone = zoneService.selectZone(id);
        zone.setDelFlag(String.valueOf(1));
        this.zoneService.updateZone(zone);

        return view;
    }
    
    /**
     * 获取区域列表
     * @param request
     * @param response
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list/all")
    public View<List<ZoneBean>> list(HttpServletRequest request, HttpServletResponse response) {
        View<List<ZoneBean>> view = new View<List<ZoneBean>>(20000, "获取列表成功");

        List<ZoneBean> zoneList = zoneService.getZoneList();
        
        view.setData(zoneList);
        
        return view;
    }
    
    /**
     * 获取区域省份两级列表
     * @param request
     * @param response
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list/province")
    public View<List<ZoneResult>> listProvince(HttpServletRequest request, HttpServletResponse response) {
        View<List<ZoneResult>> view = new View<List<ZoneResult>>(20000, "获取列表成功");

        List<ZoneResult> zoneResultList = zoneService.getZoneProvinceList();
        
        view.setData(zoneResultList);
        
        return view;
    }
}